/*
 * Author: Christopher Yeaman
 * Project: ManuscriptPR
 * Class: TCSS 360, Tenenberg
 * Date: Autumn 2011
 */

package document;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import user.User;

/**
 * Abstract class that represents documents used in the ManuscriptPR program. Each document 
 * will have a user assigned to it, a title, and a date in which the document was submitted.
 * 
 * @author Christopher Yeaman
 * @author Tony Butterfield (co-author on javadoc)
 * @version 12/21/2011
 */
public abstract class AbstractDocument implements Document
{
  /** The user that owns this document. */
  private User my_user;

  /** The file name of the document. */
  private String my_file_name;

  /**
   * Constructor for the abstract document class.
   * 
   * @param the_user the owner of the document
   * @param the_file_name the name of the document file
   */

  public AbstractDocument(final User the_user, final String the_file_name)
  {
    my_user = the_user;
    my_file_name = the_file_name;
  }

  /**
   * Retrieves the filename of the document.
   * 
   * @return the filename
   */
  public String getFileName()
  {
    return my_file_name;
  }

  /**
   * Retrieves the author of the document.
   * 
   * @return the author.
   */
  public User getUser()
  {
    return my_user;
  }

  
  /**
   * Copies a file from the file path indicated to persistent store.
   * 
   * @param the_file_name The name of the file to be saved.
   * 
   * @return Returns true if the document saved to the persistent store; otherwise false.
   */
//  public boolean saveDoc(final String the_file_name)
//  {
//    try
//    {
//
//      final FileOutputStream fout = 
//          new FileOutputStream("PersistentStore/" + the_file_name);
//      //writes the data to a file.
//      fout.write(my_user.getFullName().getBytes());
//      fout.write(the_file_name.getBytes());
//      fout.write(getText().getBytes());
//      fout.close();
//
//    } 
//    catch (final FileNotFoundException e)
//    {
//      e.printStackTrace();
//      return false;
//    } 
//    catch (final IOException e)
//    {
//      e.printStackTrace();
//      return false;
//    }
//
//    return true;
//  }
  
  /**
   * Given a file, this method will copy the file to the persistent store.
   * 
   * @param the_file The file to be copied into the persistent store.
   * 
   * @return Returns true if the document has been properly copied to the persistent
   * store; otherwise false.
   */
  public boolean saveDoc(final File the_file)
  {
    final File source = new File(the_file.getAbsolutePath());
    final File dest = new File("PersistentStore/" + my_file_name);
    
    return source.renameTo(new File(dest, my_file_name));
  }

  /**
   * This method grabs the text of any file, given its file path, and returns 
   * the text as a string.
   * 
   * @param the_file_path The relative file path of the document.
   * 
   * @return A string of the text contained within the file.
   */
  public String getText(final String the_file_path)
  {
    final File aFile = new File(the_file_path);
    final StringBuilder text = new StringBuilder();

    try
    {
      final BufferedReader br = new BufferedReader(new FileReader(aFile));
      String line = null;
      while ((line = br.readLine()) != null)
      {
        text.append(line);
        text.append(System.getProperty("line.separator"));
      }
      br.close();
    }
    catch (final FileNotFoundException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
    catch (final IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return text.toString();
  }

//  // removes any invalid characters from a string that cannot be used in a filename.
//  private String acceptableFilePath(final String the_name)
//  {
//    String lower_name = the_name.toLowerCase();
//    String new_name = "";
//    for(int i = 0; i < the_name.length(); i++)
//    {           //Between a and z in the ascii tables
//      if(lower_name.charAt(i) > 96 && lower_name.charAt(i) < 123)  
//      {
//        new_name.concat(lower_name.charAt(i)+"");
//      }
//    }
//    new_name.concat(".txt");
//    return new_name;
//  }
}
